package Function;

import interfaces.DALException;

import java.awt.print.Book;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao_Impl.MySQLBookDAO;
import dao_Impl.MySQLFakturaDAO;
import dao_Impl.MySQLSearchDAO;
import dao_Impl.MySQLUserDAO;
import dto_objects.AuthorDTO;
import dto_objects.BookDTO;
import dto_objects.SubjectDTO;
import dto_objects.UserDTO;
import java.util.regex.*;

import sun.misc.Regexp;

public class FunctionWEB{
	
	protected MySQLBookDAO SQLBook;
	protected MySQLUserDAO SQLUser;
	protected MySQLFakturaDAO SQLFaktura;
	protected MySQLSearchDAO SQLSearch;

	public FunctionWEB(){
		try{
			new connector.Connector();
			SQLBook = new MySQLBookDAO();
			SQLUser = new MySQLUserDAO();
			SQLFaktura = new MySQLFakturaDAO();
			SQLSearch = new MySQLSearchDAO();
		}catch(ClassNotFoundException|IllegalAccessException|InstantiationException|SQLException e){
			e.printStackTrace();
		}
	}
	
	public int getMaxUsrID() throws DALException, SQLException{
		return SQLUser.getMaxUserID();
	}
	
	public void createUser(UserDTO user) throws DALException{
		SQLUser.createUser(user);
	}
	
	public void updateUser(UserDTO user) throws DALException{
		SQLUser.updateUser(user);
	}
	
	public UserDTO login(String name, String pass) throws DALException{
		return SQLUser.login(name, pass);
	}
	
	public ArrayList<ArrayList> quickSearch(String search)throws DALException, SQLException{
		if(search != "" && !search.matches("^\\s*+$|[;]")){
			try{
			return SQLSearch.quickSearch(search);
			}catch(NullPointerException e){
				System.out.println("No search");
			}
		}
		
		return null;
	}
	
	public List<BookDTO> advancedSearch(String bookname, String authorFirstName, String authorLastName, String subject, String publisher) throws DALException{
		List<BookDTO> startList = SQLSearch.advancedSearch(bookname, authorFirstName, authorLastName, subject, publisher);
		List<BookDTO> endList = new ArrayList<BookDTO>();
		for(BookDTO book:startList){
			endList.add(SQLBook.getBookByISBN(book.getISBN()));
		}
		return endList;
	}
	
	public List<BookDTO> SearchISBN(int ISBN) throws DALException{
		List<BookDTO> startList = SQLSearch.SearchISBN(ISBN);
		List<BookDTO> endList = new ArrayList<BookDTO>();
		for(BookDTO book:startList){
			endList.add(SQLBook.getBookByISBN(book.getISBN()));
		}
		return endList;
	}
	
	public List<BookDTO> SearchAuthorID(int authorID) throws DALException{
		List<BookDTO> startList = SQLSearch.SearchAuthorID(authorID);
		List<BookDTO> endList = new ArrayList<BookDTO>();
		for(BookDTO book:startList){
			endList.add(SQLBook.getBookByISBN(book.getISBN()));
		}
		return endList;
	}
	
	public List<AuthorDTO> SearchAuthorIDSingle(int authorID) throws DALException{
		List<AuthorDTO> startList = SQLSearch.SearchAuthorIDSingle(authorID);
		return startList;
	}
	
	public List<BookDTO> SearchSubjectID(int subjectID) throws DALException{
		List<BookDTO> startList = SQLSearch.SearchSubjectID(subjectID);
		List<BookDTO> endList = new ArrayList<BookDTO>();
		for(BookDTO book:startList){
			endList.add(SQLBook.getBookByISBN(book.getISBN()));
		}
		return endList;
	}
	
	public List<BookDTO> SearchPublisherID(int publisherID) throws DALException{
		List<BookDTO> startList = SQLSearch.SearchPublisherID(publisherID);
		List<BookDTO> endList = new ArrayList<BookDTO>();
		for(BookDTO book:startList){
			endList.add(SQLBook.getBookByISBN(book.getISBN()));
		}
		return endList;
	}
	//TEST FUNCTION
	
	public List<BookDTO> getBookListByPublisherID(int ID){
		try {
			return SQLBook.getBooksByPublisherID(ID);
		} catch (DALException e) {
			System.out.println(e);
		}
		return null;
	}
	
	public BookDTO getBookCopyByID(int ID) throws DALException, SQLException{
		return SQLBook.getBook_CopyByID(ID);
	}
	
	public int getISBNByBookCopyID(int ID) throws DALException, SQLException{
		return SQLBook.getISBNByBook_CopyID(ID);
	}
	
	public void testAuthors(int isbn){
		try {
			List<AuthorDTO> templist = SQLBook.getAuthorsByISBN(isbn);
			for (AuthorDTO authorDTO : templist) {
				System.out.println(authorDTO.getAuthorId() + "  " + authorDTO.getAuthorFirstName() + "  " + authorDTO.getAuthorLastName());
			}
		} catch (Exception e) {
			System.out.println("empty list");
			e.printStackTrace();
		}
	}
	
	public void testSubject(int ISBN){
		try {
			List<SubjectDTO> templist = SQLBook.getSubjectByISBN(ISBN);
			for (SubjectDTO subjectDTO : templist) {
				System.out.println(subjectDTO.getSubjectID() + "  " + subjectDTO.getSubject_name());
			}
		} catch (Exception e) {
			System.out.println("empty list");
			e.printStackTrace();
		}
	}
	
	public void testBook(){
		try {
			BookDTO test = SQLBook.getBookByISBN(654321);
			System.out.println(test.getISBN()+"  "+test.getTitle());
		} catch (DALException e) {
			System.out.println("error in testbook");
			e.printStackTrace();
		}
	}
	
}
